﻿//This C# code file was released under the Ms-PL license
//http://www.opensource.org/licenses/ms-pl.html
//This script was originally intended for use with Microsoft Dynamics AX
//and maintained and distributed as a project on CodePlex
//http://dynamicsaxbuild.codeplex.com

using System;
using System.Collections.Generic;
using System.Text;
using System.Activities;
using System.IO;
using System.Text.RegularExpressions;
using Microsoft.TeamFoundation.Build.Client;

namespace AXTFSBuildLibrary
{
    [BuildActivity(HostEnvironmentOption.All)]
    public sealed class ClientConfig : CodeActivity
    {
        [RequiredArgument]
        public InArgument<string> AXConfigurationFile { get; set; }

        public OutArgument<string> ServerName { get; set; }
        public OutArgument<UInt16> PortNumber { get; set; }
        public OutArgument<string> ClientBinPath { get; set; }
        public OutArgument<string> Layer { get; set; }
        public OutArgument<string> LayerCode { get; set; }
        public OutArgument<string> LogPath { get; set; }

        protected override void Execute(CodeActivityContext context)
        {
            string filePath = AXConfigurationFile.Get(context);

            using (StreamReader streamReader = new StreamReader(File.OpenRead(filePath)))
            {
                while (!streamReader.EndOfStream)
                {
                    string line = streamReader.ReadLine().Trim();

                    if (line.IndexOf(',') < 0)
                        continue;

                    string property = line.Substring(0, line.IndexOf(',')).ToUpper();
                    string value = line.Substring(line.LastIndexOf(',') + 1);

                    switch (property)
                    {
                        case "AOS2":
                            Match match = Regex.Match(value, @"@(.+):(\d+)");  // Group0 = entire match, Group1 = server name, Group2 = port

                            if (match.Success && match.Groups.Count == 3)
                            {
                                ServerName.Set(context, match.Groups[1].Value);
                                PortNumber.Set(context, UInt16.Parse(match.Groups[2].Value));
                            }
                            break;
                        case "BINDIR":
                            value = System.Environment.ExpandEnvironmentVariables(value);
                            ClientBinPath.Set(context, value);
                            break;
                        case "AOL":
                            if (value == "")
                                value = "USR";

                            Layer.Set(context, value);
                            break;
                        case "AOLCODE":
                            LayerCode.Set(context, value);
                            break;
                        case "LOGDIR":
                            value = System.Environment.ExpandEnvironmentVariables(value);
                            LogPath.Set(context, value);
                            break;
                    }
                }
            }
        }
    }
}
